﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace _3Ex1TimeCalculator.ViewModels
{
    public class TimeCalculatorViewModel : INotifyPropertyChanged
    {
        private const string Const_Result = "Result : ";

        private string _startHour = "00";
        public String StartHour
        {
            get { return _startHour; }
            set
            {
                _startHour = value;
                RaisePropertyChanged(); 
            }
        }

        private string _startMinute = "00";
        public String StartMinute
        {
            get { return _startMinute; }
            set
            {
                    _startMinute = value;
                    RaisePropertyChanged(); 
            }
        }

        private string _endHour = "00";
        public String EndHour
        {
            get { return _endHour; }
            set
            {
                _endHour = value;
                RaisePropertyChanged(); 
            }
        }

        private string _endMinute = "00";
        public String EndMinute
        {
            get { return _endMinute; }
            set
            {
                _endMinute = value;
                RaisePropertyChanged();
            }
        }

        private string _result = Const_Result;
        public string Result
        {
            get
            {
                return _result;
            }
            set
            {
                _result = value;
                RaisePropertyChanged();
            }
        }

        public ICommand CalculateCommand { get; private set; }
        public ICommand ResetCommand { get; private set; }
        
        public TimeCalculatorViewModel ()
        {
            CalculateCommand = new SimpleCommand(Calculate);     
            ResetCommand = new SimpleCommand(Reset);
        }

        public void Calculate()
        {
            var time = new TimeCalculator();
            int calcResult = time.Calculate(StartHour,StartMinute,EndHour,EndMinute);
            Result = Const_Result + calcResult;
        }

        public void Reset()
        {
            StartHour = "00";
            StartMinute = "00";
            EndHour = "00";
            EndMinute = "00";
            Result = Const_Result;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected bool RaisePropertyChanged([CallerMemberName] String propertyName = null)
        {
            OnPropertyChanged(propertyName);
            return true;
        }

        /// <summary>
        /// Notifies listeners that a property value has changed.
        /// </summary>
        /// <param name="propertyName">Name of the property used to notify listeners.  This
        /// value is optional and can be provided automatically when invoked from compilers
        /// that support <see cref="CallerMemberNameAttribute"/>.</param>
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler eventHandler = PropertyChanged;

            if (eventHandler != null)
            {
                eventHandler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }
}
